package com.kaifamiao.jdbc.d0908;

import com.kaifamiao.jdbc.d0908.entity.Users;
import com.mysql.cj.jdbc.MysqlDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Demo03 {

    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/kfm";
        String user = "root";
        String password = "";

        MysqlDataSource dataSource = new MysqlDataSource();

        dataSource.setURL(url);
        dataSource.setUser(user);
        dataSource.setPassword(password);

        QueryRunner runner = new QueryRunner(dataSource);

        String sql = "select * from users";
        try {
            List<Users> list = runner.query(sql, new BeanListHandler<Users>(Users.class));
            list.forEach(e -> System.out.println(e));
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
